//
//  HRDynamicHeadView.m
//  Hear
//
//  Created by mac on 2017/3/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicHeadView.h"
#import "SDCycleScrollView.h"
#import "YYPhotoGroupView.h"
#import "YYControl.h"
#import "HRCoverImageModel.h"
#import "HRUpdataUserInfoModel.h"
#import "UIImage+BlurEffects.h"
@interface HRDynamicHeadView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong)SDCycleScrollView *backImageView;
@property (nonatomic, strong)YYControl *iconImageView;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)UIImageView *topImageView;
@property (nonatomic, strong)UILabel *nickNameLabel;
@property (nonatomic, strong)UILabel *numLabel;
@property(nonatomic, strong)UILabel *distanceLabel;
@property(nonatomic, strong)UIButton *label1Button;
@property(nonatomic, strong)UIButton *label2Button;
@property(nonatomic, strong)UIButton *label3Button;
@property(nonatomic, strong)UIButton *sexButton;

@end
@implementation HRDynamicHeadView




-(void)setUserInfoModel:(HRUpdataUserInfoModel *)userInfoModel{
    WeakSelf
    _userInfoModel = userInfoModel;
    NSArray *arr = [userInfoModel.UserCoverPicture jsonValueDecoded];
    if (!arr.count){
        self.backImageView.localizationImageNamesGroup = @[@"默认主页背景图"];
    }else{
        self.backImageView.imageURLStringsGroup = arr;
    }
    
    if (userInfoModel.UserPictureBackgroundColor.length){
        [HRCoverImageModel OriginImage:[UIImage imageNamed:userInfoModel.UserPictureName] scaleToSize:CGSizeMake(75, 75) callBackOriginImage:^(UIImage *originImage) {
            weakSelf.iconImageView.image = originImage;
        }];
        self.iconImageView.backgroundColor = [UIColor colorWithHexString:userInfoModel.UserPictureBackgroundColor];
    }else{
        [self.iconImageView.layer setImageWithURL:[NSURL URLWithString:userInfoModel.UserPictureName] placeholder:[UIImage imageNamed:@""]];
    }
    if ([userInfoModel.Sex intValue] == 1){
        self.sexButton.backgroundColor = CustomColor(60, 130, 200, 1);
        [self.sexButton setImage:[UIImage imageNamed:@"主页_nim_male"] forState:0];
    }else{
        self.sexButton.backgroundColor = CustomColor(240, 90, 140, 1);
        [self.sexButton setImage:[UIImage imageNamed:@"主页_nim_female"] forState:0];
    }
    [self.sexButton setTitle:userInfoModel.UserGroup forState:0];

    
    self.nickNameLabel.text = userInfoModel.UserRemark.length>0?userInfoModel.UserRemark:userInfoModel.NickName;

    self.contentLabel.text = [NSString stringWithFormat:@"%@个瞬间",userInfoModel.MomentsCount];
    
    NSArray *addressArr = [userInfoModel.Address componentsSeparatedByString:@","];
    if (addressArr.count == 3){
        self.distanceLabel.text = addressArr[1];
    }
    
    
    self.distanceLabel.hidden = [userInfoModel.LocalId isEqualToString:USERID];
    
    if ([userInfoModel.LocalId isEqualToString:USERID]){
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            
            if (weakSelf.changeBackgroudImageBlock){
                weakSelf.changeBackgroudImageBlock(self.backImageView.imageURLStringsGroup);
            }
        }];
        [self.backImageView addGestureRecognizer:tap];
    }
    
    if ([_userInfoModel.LocalId isEqualToString:@"admin"]){
        self.backImageView.timeCount = 100;
    }
    
    self.numLabel.text = [NSString stringWithFormat:@"ID：%@",userInfoModel.UserNo];
    
    NSArray *userTagsArr = [userInfoModel.UserTags jsonValueDecoded];
    for (int i =0; i <userTagsArr.count; i ++) {
        if (i ==0){
            [self.label1Button setTitle:userTagsArr[i] forState:0];
            self.label1Button.hidden = NO;
        }else if (i ==1){
            [self.label2Button setTitle:userTagsArr[i] forState:0];
            self.label2Button.hidden = NO;
        }else{
            [self.label3Button setTitle:userTagsArr[i] forState:0];
            self.label3Button.hidden = NO;
        }
    }
}





-(instancetype)initWithFrame:(CGRect)frame andCurrentUser:(NSString *)currentUserID
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        
        
        
        SDCycleScrollView *backImageView = [SDCycleScrollView new];
        [self addSubview:backImageView];
        backImageView.currentUserID = currentUserID;
        backImageView.delegate = self;
        self.backImageView = backImageView;
        backImageView.autoScroll = NO;
        backImageView.placeholderImage = [UIImage imageNamed:@"默认加载图"];
//        backImageView.currentPageDotImage = [UIImage imageNamed:@"轮播选中"];
//        backImageView.pageDotImage = [UIImage imageNamed:@"轮播未选中"];
                
        self.iconImageView = [YYControl new];
        self.iconImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:self.iconImageView];
        self.iconImageView.layer.cornerRadius = 35;
        self.iconImageView.layer.borderColor = CustomColor(230, 230, 230, 1).CGColor;
        self.iconImageView.layer.borderWidth = 3;
        self.iconImageView.clipsToBounds = YES;
        UITapGestureRecognizer *iconTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconTap)];
        [self.iconImageView addGestureRecognizer:iconTap];
        self.iconImageView.userInteractionEnabled = YES;
        
        
        self.sexButton = [UIButton new];
        [self addSubview:self.sexButton];
        self.sexButton.layer.cornerRadius = 5;
        self.sexButton.clipsToBounds = YES;
        [self.sexButton setTitleColor:[UIColor whiteColor] forState:0];
        self.sexButton.titleLabel.font = CustomUIFont(12);
        self.sexButton.enabled = NO;
        
        self.nickNameLabel = [UILabel new];
        self.nickNameLabel.textColor = FirstTextColor;
        self.nickNameLabel.font = CustomUIFont(15);
        [self addSubview:self.nickNameLabel];
        
        
        self.distanceLabel = [UILabel new];
        self.distanceLabel.textColor = BlueColor;
        self.distanceLabel.font = CustomUIFont(13);
        [self addSubview:self.distanceLabel];
//        self.distanceLabel.text = @"1KM以内";
        
        self.contentLabel = [UILabel new];
        self.contentLabel.textColor = [UIColor whiteColor];
        self.contentLabel.font = CustomUIFont(13);
        [self addSubview:self.contentLabel];
        self.contentLabel.textAlignment = 1;
        
        
        self.numLabel = [UILabel new];
        self.numLabel.textColor = FirstTextColor;
        self.numLabel.font = CustomUIFont(13);
        [self addSubview:self.numLabel];
        
        
        self.label1Button = [UIButton buttonWithType:0];
        [self.label1Button setTitleColor:[UIColor whiteColor] forState:0];
        self.label1Button.titleLabel.font = CustomUIFont(13);
        [self addSubview:self.label1Button];
        self.label1Button.layer.cornerRadius = 3;
        self.label1Button.layer.borderWidth = 1;
        self.label1Button.layer.borderColor = FirstTextColor.CGColor;
        self.label1Button.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        
        self.label2Button = [UIButton buttonWithType:0];
        [self.label2Button setTitleColor:[UIColor whiteColor] forState:0];
        self.label2Button.titleLabel.font = CustomUIFont(13);
        [self addSubview:self.label2Button];
        self.label2Button.layer.cornerRadius = 3;
        self.label2Button.layer.borderWidth = 1;
        self.label2Button.layer.borderColor = FirstTextColor.CGColor;
        self.label2Button.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];

        self.label3Button = [UIButton buttonWithType:0];
        [self.label3Button setTitleColor:[UIColor whiteColor] forState:0];
        self.label3Button.titleLabel.font = CustomUIFont(13);
        [self addSubview:self.label3Button];
        self.label3Button.layer.cornerRadius = 3;
        self.label3Button.layer.borderWidth = 1;
        self.label3Button.layer.borderColor = FirstTextColor.CGColor;
        self.label3Button.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.label1Button.hidden = YES;
        self.label2Button.hidden = YES;
        self.label3Button.hidden = YES;

        WeakSelf
        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(weakSelf);
            make.bottom.equalTo(weakSelf).offset(-70);
        }];
            
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf).offset(12);
            make.top.equalTo(weakSelf.backImageView.mas_bottom).offset(-40);

//            make.right.equalTo(weakSelf.backImageView.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(70, 70));
        }];
        
        [self.sexButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.iconImageView).offset(10);
            make.right.equalTo(weakSelf.iconImageView).offset(-10);
            make.top.equalTo(weakSelf.iconImageView.mas_bottom).offset(-10);
            make.height.mas_equalTo(18);
        }];
        
        
        [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.backImageView.mas_bottom).offset(5);
            make.width.equalTo(@150);
            make.left.equalTo(weakSelf.iconImageView.mas_right).offset(10);
            make.height.mas_equalTo(20);
        }];
        
        
        [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.nickNameLabel.mas_bottom);
            make.left.equalTo(weakSelf.nickNameLabel);
            make.size.mas_equalTo(CGSizeMake(80, 20));
        }];
        
        
        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.nickNameLabel.mas_top);
            make.width.equalTo(@80);
            make.right.equalTo(weakSelf).offset(-5);
            make.height.mas_equalTo(20);
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.backImageView.mas_bottom).offset(-30);
            make.width.equalTo(@80);
            make.right.equalTo(weakSelf).offset(-5);
            make.height.mas_equalTo(20);
        }];
        
        
        [self.label1Button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.iconImageView.mas_right);
            make.top.equalTo(weakSelf.contentLabel.mas_top);
            make.width.equalTo(@60);
            make.height.mas_equalTo(20);
        }];
        
        [self.label2Button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.label1Button.mas_right).offset(5);
            make.top.equalTo(weakSelf.label1Button.mas_top);
            make.width.equalTo(weakSelf.label1Button.mas_width);
            make.height.equalTo(weakSelf.label1Button.mas_height);
        }];
        [self.label3Button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.label2Button.mas_right).offset(5);
            make.top.equalTo(weakSelf.label1Button.mas_top);
            make.width.equalTo(weakSelf.label1Button.mas_width);
            make.height.equalTo(weakSelf.label1Button.mas_height);
        }];
    }
    return self;
}


- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    [self clickImageAtIndex:index];
}

- (void)clickImageAtIndex:(NSUInteger)index{
    YYControl *currentImageView = self.backImageView.currentImageView;
    YYPhotoGroupItem *item = [YYPhotoGroupItem new];
    NSArray *arr = [_userInfoModel.UserCoverPicture jsonValueDecoded];
    item.largeImageURL = [NSURL URLWithString:arr[index]];
    if (!arr)return;
    YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:@[item]];
    [v presentFromImageView:currentImageView toContainer:WINDOW animated:YES completion:nil];
}


- (void)iconTap{
    YYPhotoGroupItem *item = [YYPhotoGroupItem new];
    item.largeImageURL = [NSURL URLWithString:_userInfoModel.UserPictureName];
    YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:@[item]];
    [v presentFromImageView:self.iconImageView toContainer:WINDOW animated:YES completion:nil];
}


- (void)reloadBackImageView{
    HRUpdataUserInfoModel *infoModel = [HRUpdataUserInfoModel modelWithJSON:[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"]];
    self.backImageView.imageURLStringsGroup = [infoModel.UserCoverPicture jsonValueDecoded];
}


@end
